<?php

namespace App\Winged;

use App\Winged\Auth\WingedGuard;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class WingedServiceProvider extends
    \Illuminate\Support\ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerWingedAuthGuard();
    }

    /**
     * Boot the service provider.
     *
     * @return void
     */
    public function boot(Router $router)
    {
        // 覆盖laravel-admin的默认视图
        app('view')->replaceNamespace('admin', resource_path('views/admin'));
        // 语言包
        $this->loadTranslationsFrom(resource_path('lang/admin'), 'admin');

        app('view')->replaceNamespace('winged', resource_path('views/winged'));

        app('url')->forceScheme(config('winged.backend.scheme'));

        $router->pattern('id', '[0-9]+');
    }

    protected function registerRoutes(\Closure $callback, $attributes = [])
    {

    }

    /**
     * 注册系统auth
     */
    private function registerWingedAuthGuard()
    {
        $name = 'winged';

        Auth::extend($name, function () use ($name) {

            $provider = Auth::createUserProvider('winged_user');

            $guard = new WingedGuard($name, $provider, app('session.store'));

            // When using the remember me functionality of the authentication services we
            // will need to be set the encryption instance of the guard, which allows
            // secure, encrypted cookie values to get generated for those cookies.
            if (method_exists($guard, 'setCookieJar')) {
                $guard->setCookieJar(app('cookie'));
            }

            if (method_exists($guard, 'setDispatcher')) {
                $guard->setDispatcher(app('events'));
            }

            if (method_exists($guard, 'setRequest')) {
                $guard->setRequest(app()->refresh('request', $guard, 'setRequest'));
            }
            return $guard;
        });
    }


}
